package com.d3works.apps.bpm.listener;

import com.d3works.apps.baml.tools.BpmTools;
import com.d3works.apps.bpm.context.WkfCache;
import com.d3works.apps.bpm.db.WkfProcessConfig;
import java.util.HashMap;
import javax.persistence.PostPersist;
import javax.persistence.PostRemove;
import javax.persistence.PostUpdate;

public class WkfProcessConfigListener {

  @PostPersist
  @PostUpdate
  public void onSave(WkfProcessConfig config) {

    String tenantId = BpmTools.getCurentTenant();

    if (!WkfCache.WKF_MODEL_CACHE.containsKey(tenantId)) {
      WkfCache.WKF_MODEL_CACHE.put(tenantId, new HashMap<Long, String>());
    }

    WkfCache.WKF_MODEL_CACHE.get(tenantId).put(config.getId(), config.getModel());
  }

  @PostRemove
  public void onRemove(WkfProcessConfig config) {

    String tenantId = BpmTools.getCurentTenant();

    if (WkfCache.WKF_MODEL_CACHE.containsKey(tenantId)) {
      WkfCache.WKF_MODEL_CACHE.get(tenantId).remove(config.getId());
    }
  }
}
